package Controllers

import (
	"fmt"
	"log"
	_ "net/url"
	"os"
	"path"
	"strconv"
	"yz-go/Common"

	"time"
	// "github.com/pkg/errorx"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gin-gonic/gin"
)

type OMRS struct {
	CreatedAt int64
	PlayDesc  string
	Phone     int
	Realname  string
}

func ExpResume(c *gin.Context) {
	id, _ := c.Get("id")
	f := excelize.NewFile()

	f.SetCellStr("Sheet1", "A1", "报名时间")
	f.SetCellStr("Sheet1", "B1", "玩法选择")
	f.SetCellStr("Sheet1", "C1", "电话号码")
	f.SetCellStr("Sheet1", "D1", "姓名")

	omrs := []OMRS{
		{
			CreatedAt: 1714301624,
			PlayDesc:  "PlayDesc",
			Phone:     13222222222,
			Realname:  "Realname22",
		},
		// {PlayDesc: "PlayDesc"},
		// {Phone: 13222222222},
		// {Realname: "Realname"},
	}

	fmt.Printf("%#v", omrs)
	// return
	for i, v := range omrs {
		fmt.Println(i)
		if v.CreatedAt != 0 {
			t := time.Unix(v.CreatedAt, 0)
			f.SetCellStr("Sheet1", fmt.Sprintf("A%d", i+2), t.Format("2006-01-02 15:04:05"))
		} else {
			f.SetCellStr(
				"Sheet1",
				fmt.Sprintf("A%d", i+2),
				"",
			)
		}

		f.SetCellStr(
			"Sheet1",
			fmt.Sprintf("B%d", i+2),
			v.PlayDesc,
		)

		f.SetCellInt(
			"Sheet1",
			fmt.Sprintf("C%d", i+2),
			v.Phone,
		)

		f.SetCellStr(
			"Sheet1",
			fmt.Sprintf("D%d", i+2),
			v.Realname,
		)
	}
	// c.Writer.Header().Set("Content-Type", "text/plain")
	// disposition := fmt.Sprintf("attachment; filename=\"%s.xlsx\"", url.QueryEscape("某比赛名单"))
	// c.Writer.Header().Set("Content-Disposition", disposition)

	// c.Writer.WriteHeader(200)

	// _, e := f.WriteTo(c.Writer)
	// if e != nil {
	// 	c.JSON(200, gin.H{
	// 		"errno":  1,
	// 		"errmsg": e.Error(),
	// 	})
	// 	return
	// }
	fmt.Println(id)

	// 将文件保存到本地文件夹
	// 获取当前时间
	currentTime := time.Now()

	// 格式化当前时间，生成文件夹名
	folderName := "storage/resume/" + currentTime.Format("20060102")
	// folderName := fmt.Sprintf("storage/resume/%s", currentTime.Format("20060102"))
	// fmt.Println(folderName)
	// return

	// 创建文件夹
	err := os.Mkdir(folderName, 0755)
	if err != nil {
		fmt.Println("创建文件夹失败：", err)
		return
	}
	dateDir := folderName + "/your_file.xlsx"
	err = f.SaveAs(dateDir)
	if err != nil {
		log.Fatal(err)
	}
	// fmt.Println("Excel file saved successfully.")
}

func UploadFile(c *gin.Context) {

	file, err := c.FormFile("file")
	if err == nil {
		// 获取文件后缀，判断文件名
		extName := path.Ext(file.Filename)

		extArr := map[string]bool{
			".jpg":  true,
			".jpeg": true,
			".png":  true,
			".gif":  true,
		}

		// extArr2 := make(map[string]bool)
		// extArr2[".mp4"] = true

		if _, ok := extArr[extName]; !ok {
			Common.ErrorResponse(c, Common.ERROR, "上传文件不合法")
			return
		}

		// dir := fmt.Sprintf("/storage/file/%s", Common.GetDate())
		// fmt.Println(dir)

		// 格式化当前时间，生成文件夹名
		dir := "storage/file/" + Common.GetDate()

		// 创建文件夹
		err := os.MkdirAll(dir, 0755) // 多目录创建 ，使用MkdirAll
		if err != nil {

			errMsg := fmt.Sprintf("创建文件夹失败：%s", err)
			Common.ErrorResponse(c, Common.ERROR, errMsg)
			return
		}

		// 生成文件名 和 保存目录
		unixTime := Common.GetUnix()                          // 64位
		fileName := strconv.FormatInt(unixTime, 10) + extName // int转字符串 64位 10进制

		// 执行上传
		dst := path.Join(dir, fileName)
		fmt.Println(dst) // storage/file/20240429/1714389768.jpg
		c.SaveUploadedFile(file, dst)
	}
}
